<template>
  <div class="asset-list-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h2>
          <el-icon><Box /></el-icon>
          资产管理
        </h2>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>资产管理</el-breadcrumb-item>
          <el-breadcrumb-item>资产列表</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="header-actions">
        <el-button type="primary" @click="handleCreate">
          <el-icon><Plus /></el-icon>
          新增资产
        </el-button>
        <el-button @click="handleImport">
          <el-icon><Upload /></el-icon>
          导入
        </el-button>
        <el-button @click="handleExport">
          <el-icon><Download /></el-icon>
          导出
        </el-button>
        <el-button @click="handlePrint">
          <el-icon><Printer /></el-icon>
          打印标签
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <el-row :gutter="20" class="stats-cards">
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-icon" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
            <el-icon><Box /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.total }}</div>
            <div class="stat-label">资产总数</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-icon" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
            <el-icon><Money /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">¥{{ formatAmount(statistics.totalValue) }}</div>
            <div class="stat-label">资产总值</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-icon" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
            <el-icon><CircleCheck /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.inUse }}</div>
            <div class="stat-label">使用中</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-icon" style="background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);">
            <el-icon><Warning /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.needMaintenance }}</div>
            <div class="stat-label">待保养</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 搜索筛选 -->
    <el-card class="search-card" shadow="never">
      <el-form :model="searchForm" :inline="true">
        <el-form-item label="资产编号">
          <el-input v-model="searchForm.code" placeholder="请输入资产编号" clearable />
        </el-form-item>
        <el-form-item label="资产名称">
          <el-input v-model="searchForm.name" placeholder="请输入资产名称" clearable />
        </el-form-item>
        <el-form-item label="资产分类">
          <el-select v-model="searchForm.category" placeholder="请选择" clearable>
            <el-option label="办公设备" value="office" />
            <el-option label="生产设备" value="production" />
            <el-option label="IT设备" value="it" />
            <el-option label="运输工具" value="vehicle" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态">
          <el-select v-model="searchForm.status" placeholder="请选择" clearable>
            <el-option label="使用中" value="in_use" />
            <el-option label="闲置" value="idle" />
            <el-option label="维修中" value="maintenance" />
            <el-option label="已报废" value="disposed" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用部门">
          <el-select v-model="searchForm.department" placeholder="请选择" clearable>
            <el-option label="行政部" value="admin" />
            <el-option label="销售部" value="sales" />
            <el-option label="技术部" value="tech" />
            <el-option label="生产部" value="production" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><RefreshLeft /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 资产列表 -->
    <el-card class="table-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span>资产列表</span>
          <div class="header-actions">
            <el-button text @click="handleBatchTransfer" :disabled="selectedRows.length === 0">
              <el-icon><Position /></el-icon>
              批量调拨
            </el-button>
            <el-button text @click="handleBatchInventory" :disabled="selectedRows.length === 0">
              <el-icon><DocumentChecked /></el-icon>
              批量盘点
            </el-button>
            <el-button text @click="handleBatchDelete" :disabled="selectedRows.length === 0">
              <el-icon><Delete /></el-icon>
              批量删除
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="tableData"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        stripe
        border
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="code" label="资产编号" width="150" fixed>
          <template #default="{ row }">
            <el-link type="primary" @click="handleView(row)">{{ row.code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="资产名称" min-width="150" show-overflow-tooltip />
        <el-table-column prop="category" label="资产分类" width="120">
          <template #default="{ row }">
            <el-tag :type="getCategoryColor(row.category)">{{ getCategoryName(row.category) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="model" label="规格型号" width="150" show-overflow-tooltip />
        <el-table-column prop="value" label="资产价值" width="120" align="right">
          <template #default="{ row }">
            <span class="amount">¥{{ formatAmount(row.value) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="purchaseDate" label="购置日期" width="120" />
        <el-table-column prop="department" label="使用部门" width="120" />
        <el-table-column prop="user" label="使用人" width="100" />
        <el-table-column prop="location" label="存放位置" width="150" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusColor(row.status)">{{ getStatusName(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240" fixed="right">
          <template #default="{ row }">
            <el-button text type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              查看
            </el-button>
            <el-button text type="primary" size="small" @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button text type="warning" size="small" @click="handleTransfer(row)">
              <el-icon><Position /></el-icon>
              调拨
            </el-button>
            <el-button text type="danger" size="small" @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Box, Plus, Upload, Download, Printer, Money, CircleCheck, Warning,
  Search, RefreshLeft, Position, DocumentChecked, Delete, View, Edit
} from '@element-plus/icons-vue'

const router = useRouter()

// 统计数据
const statistics = reactive({
  total: 328,
  totalValue: 5680000,
  inUse: 256,
  needMaintenance: 18
})

// 搜索表单
const searchForm = reactive({
  code: '',
  name: '',
  category: '',
  status: '',
  department: ''
})

// 分页
const pagination = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})

// 表格数据
const tableData = ref([])
const loading = ref(false)
const selectedRows = ref([])

// 模拟数据
const mockData = [
  {
    id: 1,
    code: 'IT-2024-001',
    name: '联想ThinkPad笔记本',
    category: 'it',
    model: 'T14 Gen3',
    value: 8500,
    purchaseDate: '2024-01-15',
    department: '技术部',
    user: '张三',
    location: '技术部-研发室',
    status: 'in_use'
  },
  {
    id: 2,
    code: 'OF-2024-002',
    name: '办公桌椅套装',
    category: 'office',
    model: 'HJ-8802',
    value: 1500,
    purchaseDate: '2024-02-20',
    department: '行政部',
    user: '李四',
    location: '行政部-办公室',
    status: 'in_use'
  },
  {
    id: 3,
    code: 'PD-2024-003',
    name: '数控机床',
    category: 'production',
    model: 'CNC-6500',
    value: 280000,
    purchaseDate: '2024-03-10',
    department: '生产部',
    user: '王五',
    location: '生产车间-A区',
    status: 'in_use'
  },
  {
    id: 4,
    code: 'VH-2024-004',
    name: '公务用车',
    category: 'vehicle',
    model: '大众帕萨特',
    value: 230000,
    purchaseDate: '2024-01-05',
    department: '行政部',
    user: '赵六',
    location: '地下车库',
    status: 'in_use'
  },
  {
    id: 5,
    code: 'IT-2024-005',
    name: '戴尔服务器',
    category: 'it',
    model: 'PowerEdge R740',
    value: 45000,
    purchaseDate: '2024-04-01',
    department: '技术部',
    user: '钱七',
    location: '机房',
    status: 'maintenance'
  }
]

// 获取列表数据
const fetchList = () => {
  loading.value = true
  
  setTimeout(() => {
    tableData.value = mockData
    pagination.total = mockData.length
    loading.value = false
  }, 500)
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  fetchList()
}

// 重置
const handleReset = () => {
  Object.assign(searchForm, {
    code: '',
    name: '',
    category: '',
    status: '',
    department: ''
  })
  handleSearch()
}

// 新增
const handleCreate = () => {
  router.push('/asset/create')
}

// 查看
const handleView = (row) => {
  router.push(`/asset/detail/${row.id}`)
}

// 编辑
const handleEdit = (row) => {
  router.push(`/asset/edit/${row.id}`)
}

// 调拨
const handleTransfer = (row) => {
  router.push(`/asset/transfer?id=${row.id}`)
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除资产"${row.name}"吗？`,
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('删除成功')
    fetchList()
  })
}

// 批量调拨
const handleBatchTransfer = () => {
  ElMessage.info('批量调拨功能开发中')
}

// 批量盘点
const handleBatchInventory = () => {
  ElMessage.info('批量盘点功能开发中')
}

// 批量删除
const handleBatchDelete = () => {
  ElMessageBox.confirm(
    `确定要删除选中的 ${selectedRows.value.length} 项资产吗？`,
    '批量删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('批量删除成功')
    fetchList()
  })
}

// 导入
const handleImport = () => {
  ElMessage.info('导入功能开发中')
}

// 导出
const handleExport = () => {
  ElMessage.success('正在导出...')
}

// 打印标签
const handlePrint = () => {
  ElMessage.info('打印标签功能开发中')
}

// 选择变化
const handleSelectionChange = (selection) => {
  selectedRows.value = selection
}

// 分页变化
const handleSizeChange = () => {
  fetchList()
}

const handlePageChange = () => {
  fetchList()
}

// 工具函数
const formatAmount = (amount) => {
  return amount.toLocaleString('zh-CN', { minimumFractionDigits: 2 })
}

const getCategoryName = (category) => {
  const categoryMap = {
    office: '办公设备',
    production: '生产设备',
    it: 'IT设备',
    vehicle: '运输工具',
    other: '其他'
  }
  return categoryMap[category] || category
}

const getCategoryColor = (category) => {
  const colorMap = {
    office: 'primary',
    production: 'warning',
    it: 'success',
    vehicle: 'info',
    other: ''
  }
  return colorMap[category] || ''
}

const getStatusName = (status) => {
  const statusMap = {
    in_use: '使用中',
    idle: '闲置',
    maintenance: '维修中',
    disposed: '已报废'
  }
  return statusMap[status] || status
}

const getStatusColor = (status) => {
  const colorMap = {
    in_use: 'success',
    idle: 'info',
    maintenance: 'warning',
    disposed: 'danger'
  }
  return colorMap[status] || ''
}

// 页面加载
onMounted(() => {
  fetchList()
})
</script>

<style scoped>
.asset-list-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.header-left h2 {
  margin: 0 0 10px 0;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 24px;
  color: #303133;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.stats-cards {
  margin-bottom: 20px;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 20px;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: white;
  margin-right: 15px;
  flex-shrink: 0;
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.amount {
  font-weight: 600;
  color: #67c23a;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>

